package dasregistry;

import java.sql.Connection;
import java.sql.SQLException;

import org.biojava.dasobert.dasregistry.Das1Validator;
import org.biojava.services.das.registry.DasRegistry;
import org.biojava.services.das.registry.DasRegistrySql;
import org.springframework.beans.factory.BeanFactory;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

import uk.ac.sanger.dasregistry.timer.CleanRegistry;
import uk.ac.sanger.dasregistry.timer.Mirroring;

public class serverBean {
private String sourcesPath;
private String description;



public String getDescription() {
	return description;
}


public void setDescription(String description) {
	this.description = description;
}


public String getSourcesPath() {
	return sourcesPath;
}


public void setSourcesPath(String sourcesPath) {
	this.sourcesPath = sourcesPath;
}


public serverBean(){
	sourcesPath="";
	description="";
}

public String storeInDb(String open_id){
	//test sources document
	
	//store in database and then call the mirroring object to mirror the source
	
if(sourcesPath==null || sourcesPath==""){
	return "You need to specify a source for your server";
	
}
if(!sourcesPath.startsWith("http://")){
	return "The server path needs to start with http://";
}

if(!sourcesPath.endsWith("sources")){
	return "The server path needs the sources part on the end to specify your sources document";
}
if(description==null || description==""){
	return "You need to give a description of your server/institution name";
}


Das1Validator validator = new Das1Validator();

			boolean isValid = true;//validator.validateSourcesCmdShallow(sourcesPath);
if(!isValid)return "The xml returned from the sources request is not valid "+validator.getValidationMessage();

	DasRegistry registry=this.getRegistry();
	try {
		String errors=registry.storeServer(sourcesPath, open_id, description);
		BeanFactory ctx=(new ClassPathXmlApplicationContext("SpringDAO.xml"));
		CleanRegistry clean=(CleanRegistry)ctx.getBean("clean");
		boolean cleanedOk=clean.cleanServers();
		if(!cleanedOk)errors+="something went wrong when cleaning the registry";
		if(errors!=""){
			return errors;
		}
		
		//need to have a page which gives back a passkey
		//server can post there sources document to the registry with the passkey in the header this shows the user has the authority to request server ownership 
		//the registry will then mirror the sources specified by the sources document and allocate ownership to the user of those sources as well as the current user and the admin email in the registy table currently  if they already exist
		//and this will then return the sources with the DS_ids
		
		//so this bit will also be called when post or when refresh my mirror page is called
		//Mirroring mirror=new Mirroring();
		//mirror.mirrorServer(sourcesPath);
		return "";
	} catch (SQLException e) {
		// TODO Auto-generated catch block
		return ""+e.getMessage();
	}
	
	

	
}

/** return a DasRegistry object. Can be either the AxisClient to
 * communicate via SOAP (axis) or the SQLRegistry class */
private DasRegistry getRegistry(){

	BeanFactory ctx=(new ClassPathXmlApplicationContext("SpringDAO.xml"));
	return (DasRegistrySql)ctx.getBean("registryBean");
}

}
